﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;
using System.Reflection;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Template.Template
{
    public partial class DocList : System.Web.UI.Page
    {

        protected FrameHelper _framePage;

        protected KindLogic _kindLogic;

        public string _kind;
        public string _typeid;
        public string _title;
        public string _acthor;

        protected void Page_Load(object sender, EventArgs e)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                CmsTemplatePage upage = session.Get<CmsTemplatePage>(Int32.Parse(Request["tempid"]));

                //初始化全局变量值
                _kind = upage.DocKind;


                _title = String.IsNullOrEmpty(Request["doctitle"]) ? "" : Request["doctitle"];
                _acthor = String.IsNullOrEmpty(Request["acthor"]) ? "" : Request["acthor"];
                _typeid = String.IsNullOrEmpty(Request["typeid"]) ? "" : Request["typeid"];

                //初始化控件值
                ParamKit.Bind("TreeDefType", ddl_typeid, new List<ParamFilterCondition> { new ParamFilterCondition() { Code = "Kind", Value = _kind } }, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");

                tb_search_kind.Text = _kind;

                tb_search_title.Text = _title;
                tb_search_author.Text = _acthor;
                ddl_typeid.SelectedValue = _typeid;
                tempid.Text = Request["tempid"];
                _framePage = new FrameHelper(this);

                _kindLogic = new KindLogic(_kind);

                int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;
                int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;


                string hql = "from CmsDocInfo a, CmsTemplateDoc b where a.Id=b.DocId and b.TemplatePagesId=:tempid and a.Kind=:kind";
                if (!string.IsNullOrEmpty(_title.Trim()))
                {
                    hql += " and a.Title like :title";
                    //crt.Add(Expression.Like("Title", "%" + _title + "%"));
                }

                if (!string.IsNullOrEmpty(_acthor.Trim()))
                {
                    hql += " and a.Author like :auth";
                    //crt.Add(Expression.Like("Author", "%" + _acthor + "%"));
                }

                if (!string.IsNullOrEmpty(_typeid.Trim()) && _typeid.Trim() != "-1")
                {
                    hql += " and a.TypeId like :type";
                    //crt.Add(Expression.Eq("TypeId", int.Parse(_typeid)));
                }
                IQuery query = session.CreateQuery("select a " + hql + " order by a.ShowOrder, a.UpdateDate desc")

                    .SetParameter("tempid", Int32.Parse(Request["tempid"]))
                    .SetParameter("kind", _kind);
                if (!string.IsNullOrEmpty(_title.Trim()))
                {
                    query.SetParameter("title", "%" + _title + "%");
                }

                if (!string.IsNullOrEmpty(_acthor.Trim()))
                {
                    query.SetParameter("acth", "%" + _acthor + "%");
                }

                if (!string.IsNullOrEmpty(_typeid.Trim()) && _typeid.Trim() != "-1")
                {
                    query.SetParameter("type", "%" + _typeid + "%");
                }
                gridArticle.DataSource = query.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).List();

                query = session.CreateQuery("select count(*) " + hql + " order by a.ShowOrder, a.UpdateDate desc")

                    .SetParameter("tempid", Int32.Parse(Request["tempid"]))
                    .SetParameter("kind", _kind);
                if (!string.IsNullOrEmpty(_title.Trim()))
                {
                    query.SetParameter("title", "%" + _title + "%");
                }

                if (!string.IsNullOrEmpty(_acthor.Trim()))
                {
                    query.SetParameter("acth", "%" + _acthor + "%");
                }

                if (!string.IsNullOrEmpty(_typeid.Trim()) && _typeid.Trim() != "-1")
                {
                    query.SetParameter("type", "%" + _typeid + "%");
                }
                _framePage.RecordCount = Convert.ToInt32(query.UniqueResult());
            }
            gridArticle.DataBind();

        }

        /// <summary>
        /// 获取config的属性值
        /// </summary>
        /// <returns></returns>
        protected bool GetConfigPropertyValue(String field)
        {
            PropertyInfo property = _kindLogic.Config.GetType().GetProperty(field);
            return (bool)property.GetValue(_kindLogic.Config, null);
        }

 
    }
}